﻿using System;
using System.Linq;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Controllers
{
    public class ProjectController : Controller
    {
        //
        // GET: /Project/
        readonly TuanAnhEntities _db = new TuanAnhEntities();
        public int PageSize = 9;
        public static int Localpage;
        public static int pjId;
       
        [HttpGet]
        public ActionResult Index()
        {
            return View();
            
        }

   
        [HttpGet]
        public PartialViewResult ListProject(  int page = 1, int ptype = 1)
        {
            var listProjects = _db.TUANANH__PROJECT.Where(x => x.PTYPE == ptype).OrderByDescending(c => c.CREATEDATE);
            var projects = listProjects.Skip((page - 1) * PageSize).Take(PageSize).AsEnumerable();
            var viewModel = new ProjectListViewModel()
                                {
                                    Projects = projects,
                                    PagingInfo = new PagingInfo
                                                     {
                                                         CurrentPage = page,
                                                         ItemsPerPage = PageSize,
                                                         TotalItems = listProjects.Count()
                                                     }

                                };
            return PartialView("ListProject", viewModel);
        }


        [HttpPost]
        public PartialViewResult ListProject( int page = 1)
        {
            int ptype = Convert.ToInt32(Request.Form["keyFilter"]);
            var listProjects = _db.TUANANH__PROJECT.Where(x => x.PTYPE == ptype).OrderByDescending(c => c.CREATEDATE);
            var projects = listProjects.Skip((page - 1) * PageSize).Take(PageSize).AsEnumerable();
            var viewModel = new ProjectListViewModel()
            {
                Projects = projects,
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = listProjects.Count()
                }

            };
            return PartialView("ListProject", viewModel);
        }

        public ActionResult GetProjects()
        {
            var list = _db.TUANANH__PROJECT.OrderByDescending(x=>x.CREATEDATE).Take(6);
            return View(list);
        }

        public ActionResult DetailProject(int projectId, int page = 1)
        {
            Localpage = page;
            var project = _db.TUANANH__PROJECT.SingleOrDefault(x => x.PROJECT_ID == projectId);
            var listcomments = _db.TUANANH__COMMENT.Where(x => x.PROJECT_ID == projectId).OrderByDescending(c => c.DATE);
            ViewBag.CommentsCount = listcomments.Count();
            return View(project);
            
        }

        public ActionResult CommentProject(int projectId)
        {
            pjId = projectId;
            int page = Localpage;
            int Sizepage = PageSize/2 + 3;
            var listcomments = _db.TUANANH__COMMENT.Where(x => x.PROJECT_ID == projectId).OrderByDescending(c=>c.DATE);
            var comments = listcomments.Skip((page - 1) * (Sizepage)).Take(Sizepage).AsEnumerable();
            var viewModel = new CommentListViewModel()
            {
                ProjectId = projectId,
                Comments = comments,
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = Sizepage,
                    TotalItems = listcomments.Count()
                }

            };
            return View(viewModel);
        }

        public ActionResult Comment()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Comment(TUANANH__COMMENT comment)
        {
            comment.DATE = DateTime.Now;
            comment.PROJECT_ID = pjId;
            comment.USERNAME = User.Identity.Name;
            _db.TUANANH__COMMENT.AddObject(comment);
            _db.SaveChanges();
            return View();
        }
    }
}
